package com.besttone.util;

import com.besttone.bean.BusinessTypeByDayBean;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by zhengzhenzhen on 2016/11/16.
 */
public class HbaseTest {

    /**
    public void queryTable()throws IOException{

        String home = System.getProperty("hadoop.home.dir");
        System.out.println("home="+home+","+Shell.getHadoopHome());
        Table table=null;
        Connection connection=null;
        try {
            connection = ConnectionFactory.createConnection(conf);// HBase 0.99+
            table = connection.getTable(TableName.valueOf("tb_sms_day"));
            Get get = new Get(Bytes.toBytes("d6d4336210da46a796f13162507e8def"));
            Result result=table.get(get);
            for (Cell cell: result.listCells()) {
                cell.getValueArray();
                System.out.println("-------------------------------------------");
            }
        }catch(IOException e){

        }finally {
            // close everything down
            if (table != null) table.close();
            if (connection != null) connection.close();
        }
    }
    **/
    public static void main(String[] args) {
        try {
             Configuration  conf = HBaseConfiguration.create();
                conf.set("hbase.zookeeper.quorum", "172.16.0.139,172.16.0.189,172.16.0.222,172.16.0.237,172.16.0.25");
                conf.set("hbase.zookeeper.property.clientPort", "2181");
                conf.set("hadoop.home.dir","hdfs://172.16.0.166:9000/hbase");
            Connection connection = ConnectionFactory.createConnection(conf);
            //Configuration conf1=connection.getConfiguration();
           // System.out.println(conf1.get("hadoop.home.dir"));
            Table table = connection.getTable(TableName.valueOf("tb_sms_day"));
            String rowKey="d6d4336210da46a796f13162507e8def";
            Get get = new Get(Bytes.toBytes(rowKey));
            Result result=table.get(get);
            List<Cell> ceList = result.listCells();
            Map<String,Cell> map= HbaseObject.getMapFromCell(ceList);
            BusinessTypeByDayBean bean=(BusinessTypeByDayBean)HbaseObject.toBean(map,BusinessTypeByDayBean.class);
            System.out.println("test end ...");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}